//
//  RosterTitleView.m
//  Gabber
//
//  Created by 惠国 陆 on 11-9-22.
//  Copyright 2011年 恒生聚源. All rights reserved.
//

#import "RosterTitleView.h"
#import "XMPPClient.h"

@implementation RosterTitleView

@synthesize avatarView=_avatarView;
@synthesize nameLabel=_nameLabel;
@synthesize statusLabel=_statusLabel;
@synthesize statusIcon=_statusIcon;

static const CGFloat    kDefaultAvatarImageWidth   = 40.0f;
static const CGFloat    kDefaultAvatarImageHeight  = 40.0f;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        
    }
    return self;
}

- (UIImageView*)avatarView {
    if (!_avatarView) {
        _avatarView = [[UIImageView alloc] init];
        _avatarView.userInteractionEnabled = YES;
        [self addSubview:_avatarView];
    }
    return _avatarView;
}

- (UILabel*)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont boldSystemFontOfSize:20.0];
        if (TTIsPad()) {
            _nameLabel.textColor = [UIColor darkGrayColor];
        }else{
            _nameLabel.textColor = [UIColor whiteColor];
        }
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.textAlignment = UITextAlignmentLeft;
        _nameLabel.lineBreakMode = UILineBreakModeTailTruncation;
        _nameLabel.adjustsFontSizeToFitWidth = YES;        
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel*)statusLabel {
    if (!_statusLabel) {
        _statusLabel = [[UILabel alloc] init];
        _statusLabel.font = [UIFont systemFontOfSize:12.0];
        if (TTIsPad()) {
            _statusLabel.textColor = [UIColor darkGrayColor];
        }else{
            _statusLabel.textColor = [UIColor whiteColor];
        }
        _statusLabel.backgroundColor = [UIColor clearColor];
        _statusLabel.textAlignment = UITextAlignmentLeft;
        _statusLabel.contentMode = UIViewContentModeTop;
        _statusLabel.lineBreakMode = UILineBreakModeTailTruncation;
        _statusLabel.numberOfLines = kTableMessageTextLineCount;        
        [self addSubview:_statusLabel];
    }
    return _statusLabel;
}

- (UIImageView*)statusIcon {
    if (!_statusIcon) {
        _statusIcon = [[UIImageView alloc] init];
        _statusIcon.contentMode = UIViewContentModeCenter;
        [self addSubview:_statusIcon];
    }
    return _statusIcon;
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat height = self.height;
    CGFloat width = self.width - (height + kTableCellSmallMargin);
    CGFloat left = 0;
    
    self.avatarView.frame = CGRectMake(0, 2, kDefaultAvatarImageWidth, kDefaultAvatarImageHeight);
    self.avatarView.backgroundColor = [UIColor whiteColor]; 
    self.avatarView.layer.borderWidth=1.0;      
    self.avatarView.layer.borderColor=[[UIColor darkGrayColor] CGColor];   
    
    left =  self.avatarView.right + kTableCellSmallMargin;
    
    CGFloat statusWidth = 16;
    
    CGFloat nameHeight = self.nameLabel.font.ttLineHeight;
    CGFloat statusHeight = self.statusLabel.font.ttLineHeight;
    CGFloat paddingY = floor((height - (nameHeight + statusHeight))/2);
    
    
    self.nameLabel.frame = CGRectMake(left , paddingY, width , nameHeight);
    self.statusIcon.frame = CGRectMake(left, self.nameLabel.bottom , statusWidth, statusWidth);  

    left  =  self.statusIcon.right + kTableCellSmallMargin;
    
    width =  width - (statusWidth + kTableCellSmallMargin);
    self.statusLabel.frame = CGRectMake(left, self.nameLabel.bottom, width, statusHeight);
    
    
}


-(void) setAvatarImage:(UIImage*) image
{
    self.avatarView.image =image;
    
}

-(void) setName:(NSString *) name
{
    self.nameLabel.text = name;
    
}

-(void) setStatus:(NSString*) status
{
    self.statusLabel.text = status;
    
}

-(void) setIntShow:(NSInteger) intShow
{
    if (intShow >=3) {
        self.statusIcon.image = [UIImage imageNamed:@"online"]; 
        
    }else{
        self.statusIcon.image = [UIImage imageNamed:@"away"]; 

    }
}

-(void) setOffline
{
    self.statusIcon.image = [UIImage imageNamed:@"offline"]; 
    self.statusLabel.text = @"离线";
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

- (void)dealloc {
    
    TT_RELEASE_SAFELY(_avatarView);
    TT_RELEASE_SAFELY(_nameLabel);
    TT_RELEASE_SAFELY(_statusIcon);
    TT_RELEASE_SAFELY(_statusLabel);
    [super dealloc];
}







@end
